package cn.zzzyuan.controller;


import cn.zzzyuan.common.content.ResponseContent;
import cn.zzzyuan.common.entity.ResponseResult;
import cn.zzzyuan.entity.Content;
import cn.zzzyuan.entity.Doc;
import cn.zzzyuan.entity.Ebook;
import cn.zzzyuan.entity.vo.DocVo;
import cn.zzzyuan.service.ContentService;
import cn.zzzyuan.service.DocService;
import cn.zzzyuan.service.EbookService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 文档 前端控制器
 * </p>
 *
 * @author
 * @since 2022-04-13
 */
@RestController
@RequestMapping("/doc")
@Slf4j
public class DocController {

    private  final  EbookService ebookService;

    private final DocService docService;

    private final ContentService contentService;

    public DocController(EbookService ebookService, DocService docService, ContentService contentService) {
        this.ebookService = ebookService;
        this.docService = docService;
        this.contentService = contentService;
    }

    @PostMapping("/save")
    public ResponseResult addDoc(@RequestBody DocVo docVo) {
        Ebook ebook = ebookService.getOne(new QueryWrapper<Ebook>().eq("id", docVo.getEbookId()).select("id"));
        if (ebook != null) {
            if(docVo.getSort() == null){
                Doc one = docService.getOne(new QueryWrapper<Doc>()
                        .eq("parent", docVo.getParentId())
                        .orderByDesc("sort")
                        .last("limit 1"));
                docVo.setSort(one == null ? 0 : one.getSort() + 1);
            }
            Doc doc = Doc.builder()
                    .id(docVo.getId())
                    .ebookId(docVo.getEbookId())
                    .name(docVo.getName())
                    .parent(docVo.getParentId())
                    .sort(docVo.getSort()).build();

            docService.saveOrUpdate(doc);
            Content content = new Content();
            content.setId(doc.getId());
            content.setContent(docVo.getContent());
            contentService.saveOrUpdate(content);
            return ResponseResult.success(ResponseContent.SUCCESS.msg(),ResponseContent.SUCCESS.code(),1);
        }
        return ResponseResult.success(ResponseContent.ERROR.msg(),ResponseContent.ERROR.code(),-1);
    }
    @GetMapping("/delete")
    public ResponseResult deleteDoc(@RequestParam("id")Long id) {
        log.info("" + id);
        boolean book=docService.removeById(id);
        if(book){
            contentService.removeById(id);
            return ResponseResult.success(ResponseContent.SUCCESS.msg(),ResponseContent.SUCCESS.code(),1);
        }
        return ResponseResult.success(ResponseContent.ERROR.msg(),ResponseContent.ERROR.code(),-1);
    }
    @GetMapping("/get")
    public ResponseResult getContent(@RequestParam("id")Long id){
        Doc doc= docService.getOne(new QueryWrapper<Doc>().eq("id",id));
        if(doc!=null){
            Content content = contentService.getOne(new QueryWrapper<Content>().eq("id", id));
            DocVo data = DocVo.builder().id(doc.getId())
                    .ebookId(doc.getEbookId())
                    .parentId(doc.getParent())
                    .name(doc.getName())
                    .sort(doc.getSort())
                    .content(content.getContent())
                    .viewCount(doc.getViewCount())
                    .voteCount(doc.getVoteCount())
                    .build();
            return ResponseResult.success(ResponseContent.SUCCESS.msg(),ResponseContent.SUCCESS.code(),data);
        }
        return ResponseResult.success(ResponseContent.ERROR.msg(),ResponseContent.ERROR.code(),-1);


    }
    @GetMapping("/getAll")
    public ResponseResult getAllDoc() {
        List<Doc> docs = docService.list();
        List<DocVo> docVos = new ArrayList<>();
        if(docs!=null){
            for(Doc doc:docs){
                docVos.add(
                        DocVo.builder().id(doc.getId())
                                .ebookId(doc.getEbookId())
                                .parentId(doc.getParent())
                                .name(doc.getName())
                                .sort(doc.getSort())
                                .content(contentService.getOne(new QueryWrapper<Content>().eq("id",doc.getId())).getContent())
                                .viewCount(doc.getViewCount())
                                .voteCount(doc.getVoteCount())
                                .build()

                );
            }
            return ResponseResult.success(ResponseContent.SUCCESS.msg(),ResponseContent.SUCCESS.code(),docVos);
        }
        return ResponseResult.success(ResponseContent.ERROR.msg(),ResponseContent.ERROR.code(),-1);
    }



}

